package mine.imgproc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;

import mine.core.Cell;

/**
 * The Class Img2CellsImpl.
 * </br>
 * Remember below:
 * </br>
 * <b>Coordinate</b>: means the real pixel coordinate. </br>
 * <b>Index</b> : the index of one cell.
 */
public class Img2CellsImpl implements Img2Cells {

	private int cellWidth;
	private Dimension startPoint;
	private BufferedImage currentScreen;
	private final double unknowX = 0.5;
	private final double unknowY = 0.95;
	public Img2CellsImpl() {
		this.cellWidth = ScreenUtils.getCellWidth();
		this.startPoint = ScreenUtils.getStartPoint();
	}
	
	
	
	@Override
	public Cell[][] getCurrentChessCells() {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public Dimension getCellClickPoint(int indexX, int indexY) {
		// TODO Auto-generated method stub
		int x = startPoint.width + indexX * cellWidth + cellWidth/2;
		int y = startPoint.height + indexY * cellWidth + cellWidth/2;
		return new Dimension(x,y);
	}


	/**
	 * Gets the cell information of a specific cell.
	 *
	 * @param indexX the index x
	 * @param indexY the index y
	 * @return the cell type
	 */
	private Cell getCellType(int indexX, int indexY){
		Cell resultCell = new Cell();
		return resultCell;
	}
	

	private Dimension getCellColorPoint(int indexX, int indexY) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * Gets the pixel point for unknown test.
	 *
	 * @param indexX the index x
	 * @param indexY the index y
	 * @return the unknow point
	 */
	private Dimension getUnknowPoint(int indexX, int indexY) {
		int positionX = (int) (startPoint.width + indexX * cellWidth + cellWidth * unknowX);
		int positionY = (int) (startPoint.width + indexY * cellWidth + cellWidth * unknowY);
		return new Dimension(positionX, positionY);
	}
	/**
	 * Gets the cell color.
	 *
	 * @param coordinateX the coordinate x
	 * @param coordinateY the coordinate y
	 * @return the cell color
	 */
	private Color getPixelColor(int coordinateX, int coordinateY) {
		return new Color(currentScreen.getRGB(coordinateX, coordinateY));
	}
	
	


	@Override
	public void updateCurrentImage(BufferedImage currentScreen) {
		// TODO Auto-generated method stub
		this.currentScreen = currentScreen; 
	}

	/**
	 * For test this implementation.
	 */
	public void forTest(int coordinateX, int coordinateY) {
		Color c = getPixelColor(coordinateX,coordinateY);
		int  red = c.getRed();
		int  green = c.getGreen();
		int  blue = c.getBlue();
		System.out.println(red + " : " + green + " : " + blue );
	}

	/**
	 * Gets the if the cell is unkonwn, if it's, return unknown cell, if not return unknown, should never return null.
	 *
	 * @param indexX the index x
	 * @param indexY the index y
	 * @return the un known
	 */
	private Cell getUnknown(int indexX, int indexY) {
		Cell resultCell = new Cell();
		Dimension point = getUnknowPoint(indexX, indexY);
		Color cellColor = getPixelColor(point.width, point.height);
		if(cellColor.getRed()>=200 && cellColor.getGreen()>=200 && cellColor.getBlue()>=200) {
			resultCell.setKnown(true);
			return resultCell;
		}
		resultCell.setKnown(false);
		return resultCell;
	}
}
